package cn.wildfire.chat.app.main;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.gson.JsonObject;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponsePhoneNumberUserInfo;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfirechat.remote.ChatManager;

public class ForgetPasswordActivity extends WfcBaseActivity {

    @BindView(R2.id.rl_find_by_sms)
    RelativeLayout rlFindBySms;
    @BindView(R2.id.rl_find_by_security_password)
    RelativeLayout rlFindBySecurityPassword;
    @BindView(R2.id.rl_find_by_id_code)
    RelativeLayout rlFindByIdCode;
    @BindView(R2.id.rl_find_by_service)
    RelativeLayout rlFindByService;

    private String cid;
    private String countryName;

    ResponsePhoneNumberUserInfo responsePhoneNumberUserInfo;

    @Override
    protected void afterViews() {
        super.afterViews();
        Intent intent = getIntent();
        countryName = intent.getStringExtra("CountryName");
        responsePhoneNumberUserInfo = intent.getParcelableExtra("UserInfo");
        responsePhoneNumberUserInfo.getResult().getMobile();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.forget_password_title);
        setSearchLayoutVisibility(false);
    }

    @OnClick({R2.id.rl_find_by_sms, R2.id.rl_find_by_security_password, R2.id.rl_find_by_id_code, R2.id.rl_find_by_service})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_find_by_sms) {
            Intent intentSMS = new Intent(this, RecoveryBySMSActivity.class);
            intentSMS.putExtra("UserInfo", responsePhoneNumberUserInfo);
            intentSMS.putExtra("CountryName", countryName);
            startActivity(intentSMS);
        } else if (id == R.id.rl_find_by_security_password) {
            Intent intentSecurityPassword = new Intent(this, SecurityQuestionSubmitActivity.class);
            intentSecurityPassword.putExtra("UserInfo", responsePhoneNumberUserInfo);
            startActivity(intentSecurityPassword);
        } else if (id == R.id.rl_find_by_id_code) {//此处直接校准设备码
            checkCode();
        } else if (id == R.id.rl_find_by_service) {
            Intent intentManualAppeal = new Intent(this, ManualAppealActivity.class);
            intentManualAppeal.putExtra("UserInfo", responsePhoneNumberUserInfo);
            startActivity(intentManualAppeal);
        }
    }

    private void checkCode() {
        try {
            cid = ChatManager.Instance().getClientId();

            HashMap<String,Object> params = new HashMap<>();
            params.put("cid",cid);
            params.put("uid",responsePhoneNumberUserInfo.getResult().getUid());

            OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.REQUEST_CID_VERIFICATION, params, new ReqCallBack<Object>() {
                @Override
                public void onReqSuccess(Object result) {
                    ResponseEntity<JsonObject, String> responseEntity = new ResponseEntity<>(result.toString());
                    if (!responseEntity.getErrorCode().equals("0")){
                        ToastTip.tip(responseEntity.message);
                        return;
                    }else{
                        //如果验证成功,设置密码
                        Intent intent = new Intent(ForgetPasswordActivity.this,SetNewPasswordActivity.class);
                        intent.putExtra("UserInfo",responsePhoneNumberUserInfo);
                        startActivity(intent);
                    }
                }

                @Override
                public void onReqFailed(String errorMsg) {
                    ToastTip.tip(getString(R.string.verification_failure));
                }
            }).request();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
